﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace IFIGame
{
    public class BonJoueur : Joueur
    {
        // Gestion clavier 
        KeyboardState myKeyboardState;
        KeyboardState oldKeyboardState;
        List<Obstacle> listObstacle;

        const int MAX_NB_VIE = 10;
        int nb_vie = 5;

        public BonJoueur(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            //this.init(this.Game, 0, 0, 0,"Textures/viseur");
          
        }
        public BonJoueur(Game game, List<Obstacle> _listObstacle, int _x, int _y, int _v, string _stringTexture)
            : base(game, _x, _y, _v, _stringTexture)
        {
            listObstacle = _listObstacle;
            livres = new List<Livre>(MAX_NB_LIVRE);
        }
        public BonJoueur(Game game, int _x, int _y, int _v, string _stringTexture)
            : base(game, _x, _y, _v, _stringTexture)
        {
            //this.init(this.Game, _x, _y, _v, _stringTexture);
            livres = new List<Livre>(MAX_NB_LIVRE);
        }
        public void addNbVie(int nb_vie)
        {
            this.nb_vie = this.nb_vie + nb_vie;
            nb_vie = nb_vie > MAX_NB_VIE ? MAX_NB_VIE : nb_vie;
        }

        public List<Livre> getLivres()
        {
            return livres;
        }

        public override bool lancerLivre(Livre _livre, Joueur _joueur)
        {
            if (nb_livre >= 0)
            {
                base.lancerLivre(_livre, _joueur);
                return true;
            }
            return false;
        }

        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
           /* if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();*/

            // TODO: Add your update logic here

            // Detection des touches du clavier
            myKeyboardState = Keyboard.GetState();
            if (myKeyboardState.IsKeyDown(Keys.Up) && this.checkCollision(this.getX(),this.getY() - this.getV()) == null)
            {
               this.setY(this.getY()-this.getV());
               for (int i = 0; i < this.livres.Count; i++)
               {
                   this.livres[i].setY(this.getY() - this.getV());
               }
               this.rotate(0);
            }
            else if (myKeyboardState.IsKeyDown(Keys.Up) && this.checkCollision(this.getX(), this.getY() - this.getV()) != null)
            {
               // Obstacle obstacle = this.checkCollision(this.getX(), this.getY() - this.getV());
                //this.setY(obstacle.getHeight() + (this.getRectangle().Height/2));
            }

            if (myKeyboardState.IsKeyDown(Keys.Down) && this.checkCollision(this.getX(), this.getY() + this.getV()) == null)
            {
               this.setY(this.getY()+this.getV());
               for (int i = 0; i < this.livres.Count; i++)
               {
                   this.livres[i].setY(this.getY() + this.getV());
               }
               this.rotate(180);
            }
            else if (myKeyboardState.IsKeyDown(Keys.Down) && this.checkCollision(this.getX(), this.getY() + this.getV()) != null)
            {
              //  Obstacle obstacle = this.checkCollision(this.getX(), this.getY() + this.getV());
              //  this.setY(obstacle.getHeight() - (this.getRectangle().Height / 2));
            }

            if (myKeyboardState.IsKeyDown(Keys.Left) && this.checkCollision(this.getX() - this.getV(),this.getY()) == null)
            {
                this.setX(this.getX()-this.getV());
                for (int i = 0; i < this.livres.Count; i++)
                {
                    this.livres[i].setX(this.getX() - this.getV());
                }
                this.rotate(270);
            }
            else if (myKeyboardState.IsKeyDown(Keys.Left) && this.checkCollision(this.getX() - this.getV(), this.getY()) != null)
            {
               // Obstacle obstacle = this.checkCollision(this.getX() - this.getV(), this.getY());
              //  this.setX(obstacle.getWidth() - (this.getRectangle().Width / 2));
            }
            if (myKeyboardState.IsKeyDown(Keys.Right) && this.checkCollision(this.getX() + this.getV(), this.getY()) == null)
            {
                this.setX(this.getX() + this.getV());
                for (int i = 0; i < this.livres.Count; i++)
                {
                    this.livres[i].setX(this.getX() + this.getV());
                }
                this.rotate(90);
            }
            else if (myKeyboardState.IsKeyDown(Keys.Right) && this.checkCollision(this.getX() + this.getV(), this.getY()) != null)
            {
               // Obstacle obstacle = this.checkCollision(this.getX() + this.getV(), this.getY());
                // this.setX(obstacle.getWidth() + (this.getRectangle().Width / 2));
            }

            oldKeyboardState = myKeyboardState;
            // Exercice : Modifier le code pour bloquer l'avatar sur les coins de l'écran
            // Exercice : modifier le code pour avoir un mouvement circulaire de l'avatar (s'il sort à gauche, il entre à droite)
            base.Update(gameTime);
        }

        public Obstacle checkCollision(int x, int y)
        {
            if (listObstacle == null)
                return null;
            if (listObstacle.Count == 0)
                return null;
            Obstacle result = null;
            Rectangle rect = new Rectangle(x, y, this.getRectangle().Width, this.getRectangle().Height);
            for (int i = 0; i < listObstacle.Count; i++)
            {
                Obstacle obstacle = listObstacle.ElementAt(i);
                if (rect.Intersects(obstacle.getRectangle()))
                {
                    result = obstacle;
                    break;
                }
            }
            return result;
        }
    }
}
